/*
 * JSchoolManagerPortableApp.java
 */
package jschoolmanagerportable;

import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class JSchoolManagerPortableApp extends SingleFrameApplication {
    private static Logger log;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        try {
            File f = new File("");
            String rutaFisica = f.getAbsolutePath();
            FileHandler handler = new FileHandler(rutaFisica+"/logs/jschoolmanagerportable.log", true);

            setLog(Logger.getLogger("jschoolmanagerportable.JSchoolManagerPortableApp"));
            getLog().addHandler(handler);
        } catch (IOException e) {
            e.printStackTrace();
        }
        show(new JSchoolManagerPortableView(this));
        getLog().info("startup");
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of JSchoolManagerPortableApp
     */
    public static JSchoolManagerPortableApp getApplication() {
        return Application.getInstance(JSchoolManagerPortableApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(JSchoolManagerPortableApp.class, args);
    }

    @Override
    protected void shutdown() {
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true", "root", "jug.cl");
        } catch (SQLException ex) {
        //Tutto Normale!
            /*
         *"Shutdown commands always raise SQLExceptions". Segun documentación oficial
         * http://developers.sun.com/docs/javadb/10.3.1.4/devguide/index.html
         */
        }
    }

    public static Logger getLog() {
        return log;
    }

    private void setLog(Logger log) {
        this.log = log;
    }
}
